/**
 * PK10的逻辑处理
 */
(function() {
    var currTabId = 1;
    var tabDraws = [
        // 1：两面
        fillLM,

        // 2 单号
        fillDH,

        // 3 组合
        fillZuHe,
    ];

    function fillLM() {
        var types = ["大", "小", "单", "双"];
        var table = $(".guanya tbody");
        table.empty();
        var tr = $("<tr></tr>");
        for (var i = 0; i < types.length; i++) {
            var t = types[i];
            add_bet_item('冠亚' + t, '冠亚' + t, tr);
        }
        table.append(tr);

        var ths = ["冠军", "亚军", "第三名", "第四名", "第五名", "第六名", "第七名", "第八名", "第九名", "第十名"];
        var arr = ['冠', '亚', '三', '四', '五', '六', '七', '八', '九', '十'];
        var types = ["大", "小", "单", "双", "龙", "虎"];

        // 前5名
        var c = $(".t1-5 tbody");
        c.empty();
        var tr = $("<tr></tr>");
        c.append(tr);
        for (var i = 0; i < 5; i++) {
            var table = $('<table class="u-table2 play_tab_15"></table>');
            table.append('<thead><tr><th colspan="3">' + ths[i] + '</tr></thead>');

            var tbody = $("<tbody></tbody>");
            table.append(tbody);
            for (var j in types) {
                var tr2 = $("<tr></tr>");
                add_bet_item(arr[i] + types[j], types[j], tr2);
                tbody.append(tr2);
            }

            var td = $("<td></td>");
            td.append(table);
            tr.append(td);
        }

        // 6-10名
        var c = $(".t6-10 tbody");
        c.empty();
        var tr = $("<tr></tr>");
        c.append(tr);
        for (var i = 5; i < 10; i++) {
            var table = $('<table class="u-table2 play_tab_15"></table>');
            table.append('<thead><tr><th colspan="3">' + ths[i] + '</tr></thead>');

            var tbody = $("<tbody></tbody>");
            table.append(tbody);
            for (var j = 0; j < 4; j++) {
                var tr2 = $("<tr></tr>");
                add_bet_item(arr[i] + types[j], types[j], tr2);
                tbody.append(tr2);
            }

            var td = $("<td></td>");
            td.append(table);
            tr.append(td);
        }
    }

    function fillDH() {
        var ths = ["冠军", "亚军", "第三名", "第四名", "第五名", "第六名", "第七名", "第八名", "第九名", "第十名"];
        var arr = ['冠', '亚', '三', '四', '五', '六', '七', '八', '九', '十'];
        var types = ["大", "小", "单", "双", "龙", "虎"];

        // 单号1-5名
        var arr2 = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十'];
        var c = $(".danhao1-5 tbody");
        c.empty();
        var tr = $("<tr></tr>");
        c.append(tr);
        for (var i = 0; i < 5; i++) {
            var table = $('<table class="u-table2 play_tab_15"></table>');
            table.append('<thead><tr><th colspan="3">' + ths[i] + '</tr></thead>');

            var tbody = $("<tbody></tbody>");
            table.append(tbody);
            for (var j = 1; j <= 10; j++) {
                var tr2 = $("<tr></tr>");
                var t = '<span class="ball c-n__index__"></span>'.replace(/__index__/, j);
                add_bet_item(arr[i] + arr2[j - 1], t, tr2);
                tbody.append(tr2);
            }

            var td = $("<td></td>");
            td.append(table);
            tr.append(td);
        }
        
        // 单号6-10名
        var c = $(".danhao6-10 tbody");
        c.empty();
        var tr = $("<tr></tr>");
        c.append(tr);
        for (var i = 5; i < 10; i++) {
            var table = $('<table class="u-table2 play_tab_15"></table>');
            table.append('<thead><tr><th colspan="3">' + ths[i] + '</tr></thead>');

            var tbody = $("<tbody></tbody>");
            table.append(tbody);
            for (var j = 1; j <= 10; j++) {
                var tr2 = $("<tr></tr>");
                var t = '<span class="ball c-n__index__"></span>'.replace(/__index__/, j);
                add_bet_item(arr[i] + arr2[j - 1], t, tr2);
                tbody.append(tr2);
            }

            var td = $("<td></td>");
            td.append(table);
            tr.append(td);
        }
    }

    function fillZuHe() {
        var types = ["大", "小", "单", "双", "龙", "虎"];

        // 冠亚组合
        var table = $(".guanyahe tbody");
        table.empty();
        var tr = $("<tr></tr>");
        for (var i = 3; i <= 19; i++) {
            add_bet_item("冠亚" + i, i, tr);
            if ((i - 3) % 4 == 3) {
                table.append(tr);
                var tr = $("<tr></tr>");
            }
        }
        tr.append("<td colspan='12'></td>");
        table.append(tr);

        // 大小单双
        var tr = $("<tr></tr>");
        for (var i = 0; i < 4; i++) {
            add_bet_item("冠亚" + types[i], "冠亚" + types[i], tr);
        }
        table.append(tr);
    }

    var initF = function(game, first) {
        window.game = game;
        initGame(game, function() {
            if (first !== false) fillGameTabs(["两面盘", "单号1~10", "冠亚组合"]);
            currTabId = parseInt($("#cate_menus_50 .selected").attr("tid"));
            $("#tab" + currTabId).attr("flag", 1);
            var f = tabDraws[currTabId - 1];
            f();
        }, first);
    }

    // 绘制开奖结果
    var drawF = function(list) {
        var div = $("#result_balls");
        div.attr("class", "T_PK10 L_BJPK10");
        var l = '<span><b class="b__index__ T_PK10 L_BJPK10">__index__</b></span>';
        for (var i in list) {
            var s = l.replace(/__index__/g, parseInt(list[i]));
            div.append(s);
        }
    }

    // 检测能否下注，看下单号1-10是否满足要求
    var checkF = function(type, issue, data) {
        if (type != 'bjpk10' && type != 'zypk10') return true;

        var limit = Util.getNumberLimit(type);
        if (limit <= 0 || limit >= 10) return true;

        var arr1 = ['冠', '亚', '三', '四', '五', '六', '七', '八', '九', '十'];
        var arr2 = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十'];
        var m = {};
        for (var k in data) {
            if (k.length != 2) continue;
            if (arr1.indexOf(k[0]) != -1 && arr2.indexOf(k[1]) != -1) {
                if (!m[k[0]]) m[k[0]] = {};
                m[k[0]][k[1]] = true;
            }
        }
        if (Util.isEmpty(m)) return true;

        // 获取下已经下注的单据
        var bets = context.bets[type];
        if (bets) bets = bets[issue] || [];
        else bets = [];
        for (var i in bets) {
            var k = bets[i].target;
            if (k.length != 2) continue;
            if (arr1.indexOf(k[0]) != -1 && arr2.indexOf(k[1]) != -1) {
                if (!m[k[0]]) m[k[0]] = {};
                m[k[0]][k[1]] = true;
            }
        }

        var total = 0;

        for (var k in m) {
            var m2 = m[k];
            if (Object.keys(m2).length > limit) {
                var l = "下注失败，__g__ 定位玩法，单个名次不允许下注__v__码或者__v__以上！如有疑问，请咨询客服！";
                l = l.replace(/__g__/, get_game_display_name(type));
                l = l.replace(/__v__/g, limit);
                alert2(l, true);
                return false;
            }
        }

        return true;
    }
    
    var data = {
        drawResult: drawF,
        checkBetList: checkF,
        init: initF,
    }
    context.global.game["bjpk10"] = data;
    context.global.game["zypk10"] = data;
})();